module ConditionalClass
	
	module SubClassModule
		attr_accessor :conditions
		def init_conditions
			@conditions = []
		end
		
		def inherited(klass)
			klass.extend SubClassModule
			klass.init_conditions
			
			self.superclass.inherited klass
		end
		
	end
	
	module ClassModule
		def inherited(klass)
			@subclasses << klass
			klass.extend SubClassModule
			klass.init_conditions
		end
		
		def condition
			self.conditions << (proc do |*x| yield(*x) end)
		end
		
		def init_subclasses
			@subclasses = []
		end
		
		def each_class_match(*args)
			@subclasses.each do |klass|

				aux = true
				
				klass.conditions.each do |condition|
			
					
					if not condition.call(*args)
						aux = nil
						break
					end
				
					
				end
				
				if aux
					yield(klass)
				end
			end
		end
	end
	
	def self.included(klass)
		klass.extend ClassModule
		klass.init_subclasses
	end
end

class X
	include ConditionalClass
	
	def self.order(q)
		self.condition do |x|
			x > q
		end
	end
end

class A < X
	condition do |x|
		x % 2 == 1
	end
	
	def evaluate
		print "x es impar\n"
	end
end

class B < X
	order 3

	def evaluate
		print "x es mayor que 3\n"
	end
end

class C < B
	order 7

	def evaluate
		print "x es mayor que 7\n"
	end
end


#X.each_class_match(9) do |klass|
#	klass.new.evaluate
#end
